﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using shop.Model;
using shop.Bll;
using shop;
using WeifenLuo.WinFormsUI.Docking;
using CommonTools.Controls;

namespace shop.BasicInfo
{
    public partial class EditingClearForm : DockContent
    {
        shop.Bll.BasicInfo.EditingClearBll bll = new shop.Bll.BasicInfo.EditingClearBll();
        Editings SelectedEditing;
        string Msg="";

        public EditingClearForm()
        {
            InitializeComponent();
        }

        private void Btn_Query_Click(object sender, EventArgs e)
        {
            grid.DataSource = bll.Query();
            if (grid.DataSource == null)
            {
                //MessageTip.ShowWarning("没有数据");
                return;
            }
            if (grid.Rows.Count>0)
            {
                FormatDgv();
            }
        }

        private void FormatDgv()
        {
            grid.Columns["id"].Visible = false;
            grid.Columns["UserName"].Width = 80;
            grid.Columns["UserName"].HeaderText = "当前用户";
            grid.Columns["DocChniaName"].Width = 200;
            grid.Columns["DocChniaName"].HeaderText = "单据名称(中文)";
            grid.Columns["DocName"].Width = 200;
            grid.Columns["DocName"].HeaderText = "单据名称(英文)";
            grid.Columns["DocId"].Width = 60;
            grid.Columns["DocId"].HeaderText = "单据ID";
            grid.Columns["EditTime"].Width = 120;
            grid.Columns["EditTime"].HeaderText = "单据编辑时间";
            grid.Columns["UserComputer"].Width = 200;
            grid.Columns["UserComputer"].HeaderText = "计算机";

        }

        private void grid_SelectionChanged(object sender, EventArgs e)
        {
            if (grid.SelectedCells.Count > 0)
            {
                int index = grid.SelectedCells[0].RowIndex;
                if (index >= 0)
                {
                    SelectedEditing = (Editings)grid.Rows[index].DataBoundItem;
                }
            }
        }

        private void Btn_ClearSelect_Click(object sender, EventArgs e)
        {
            if (grid.DataSource == null || grid.Rows.Count <= 0)
            {
                return;
            }
            if (SelectedEditing==null)
            {
                MessageTip.ShowError("明细没有被选定");
                return;
            }
            DialogResult result = MessageBox.Show("是否删除单据编辑状态？", "删除单条", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                if (bll.DeleteItem(SelectedEditing, out Msg))
                {
                    MessageTip.ShowOk("删除成功");
                    Btn_Query_Click(sender, e);
                }
                else
                {
                    MessageTip.ShowWarning(Msg);
                }
            }
            
        }

        private void Btn_ClearAll_Click(object sender, EventArgs e)
        {
            if (grid.DataSource==null || grid.Rows.Count<=0)
            {
                return;
            }
            DialogResult result = MessageBox.Show("是否删除所有单据编辑状态？", "删除所有", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                bll.DeleteAllItem(out Msg);
                if (string.IsNullOrEmpty(Msg) == false)
                {
                    MessageTip.ShowWarning(Msg);
                }
                Btn_Query_Click(sender, e);
            }
        }
    }
}
